package com.qf.fmall2203.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2203.index.comments.entity.Comments;
import com.qf.fmall2203.index.comments.entity.ProductComments;
import com.qf.fmall2203.index.comments.mapper.ProductCommentsMapper;
import com.qf.fmall2203.index.comments.service.IProductCommentsService;
import com.qf.fmall2203.index.entity.DetailInfo;
import com.qf.fmall2203.index.entity.GoodPercent;
import com.qf.fmall2203.index.entity.Product;
import com.qf.fmall2203.index.entity.ProductImg;
import com.qf.fmall2203.index.mapper.ProductMapper;
import com.qf.fmall2203.index.parms.entity.ProductParams;
import com.qf.fmall2203.index.parms.mapper.ProductParamsMapper;
import com.qf.fmall2203.index.productsku.entity.ProductSkus;
import com.qf.fmall2203.index.productsku.service.IProductSkuService;
import com.qf.fmall2203.index.service.ProductImgService;
import com.qf.fmall2203.index.service.ProductService;
import com.qf.fmall2203.user.entity.Users;
import com.qf.fmall2203.user.service.IUsersService;
import com.qf.fmall2203.user.service.impl.UsersServiceImpl;
import com.qf.fmall2203.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    IProductSkuService productSkuService;
    @Autowired
    ProductImgService productImgService;
    @Autowired
    IProductCommentsService productCommentsService;
    @Autowired
    ProductCommentsMapper productCommentsMapper;
    @Autowired
    ProductParamsMapper productParamsMapper;
    @Autowired
    IUsersService usersService;
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo getDetailInfo(Integer id) {

        Product byId = this.getOne(new QueryWrapper<Product>().eq("product_id",id));

        List<ProductSkus> byId1 =  productSkuService.list(new QueryWrapper<ProductSkus>().eq("product_id",id));

        List<ProductImg> productImgs = productImgService.list(new QueryWrapper<ProductImg>().eq("item_id",id));

        DetailInfo detailInfo = new DetailInfo(byId,byId1,productImgs);

        return ResultVo.ok(detailInfo);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailCommonts(Integer id, Integer pageNum, Integer limit) {

        Page<ProductComments> userPage = new Page<>(pageNum, limit);

        Page<ProductComments> product_id = productCommentsMapper.selectPage(userPage, new QueryWrapper<ProductComments>().eq("product_id", id));

        List<ProductComments> product_id1 =product_id.getRecords();

        for (ProductComments productComments : product_id1) {

            Users byId = usersService.getById(productComments.getUserId());

            productComments.setUsername( byId.getUsername());

            productComments.setNickname(byId.getNickname());

            productComments.setUserImg(byId.getUserImg());

        }

        long l = product_id.getTotal() % limit;

        if (l==0){
             l = product_id.getTotal() / limit;
        }else {
            l = product_id.getTotal() / limit + 1;
        }

        Comments comments = new Comments(l,product_id.getTotal(),product_id1);

        return  ResultVo.ok(comments);
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailCommontscount(Integer id) {
        List<ProductComments> product_id1 = productCommentsMapper.selectList(new QueryWrapper<ProductComments>().eq("product_id", id));

        Integer good = 0;
        Integer badCom = 0;
        Integer mid = 0;
        for (ProductComments productComments : product_id1) {

            if (productComments.getCommType().equals(1)){
                good += 1;
            }
            if (productComments.getCommType().equals(-1)){
                badCom += 1;
            }
            if (productComments.getCommType().equals(0)){
                mid +=1;
            }
        }
        Integer size = product_id1.size();

        BigDecimal bigDecimal = new BigDecimal(good);

        BigDecimal bigDecimal1 = new BigDecimal(product_id1.size());

        BigDecimal divide = bigDecimal.divide(bigDecimal1,2,BigDecimal.ROUND_HALF_UP);

        GoodPercent goodPercent = new GoodPercent(good, size, badCom, mid, divide);

        ResultVo ok = ResultVo.ok(goodPercent);

        return ok;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailParams(Integer id) {

        ProductParams productParams = productParamsMapper.selectOne(new QueryWrapper<ProductParams>().eq("product_id",id));


        return ResultVo.ok(productParams);
    }

}
